/**
 * 
 */
package org.ChrisRiffle.Acme.BoundingBox.controlers;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JOptionPane;

import org.ChrisRiffle.Acme.BoundingBox.workers.QueryExecutorWorker;

/**
 * BoxQueryPanelListener
 * 
 * @author RIF
 *
 */
public class QueryPanelListener implements ActionListener {

	
	public static final String RUN_BUTTON_CLICK = "RUN_BUTTON_CLICK";
	
	
	
	@Override
	public void actionPerformed(ActionEvent e) {

		String actionCmd = e.getActionCommand(); 
		
		if(RUN_BUTTON_CLICK.equals(actionCmd)){
			
			handleRunButtonClick();
		}
	
	}
	
	

	/**
	 * handleRunButtonClick
	 */
	private void handleRunButtonClick() {

		
		Float LX = QueryPanelControler.INSTANCE.getLX();
		Float rX = QueryPanelControler.INSTANCE.getRX();
		Float uY = QueryPanelControler.INSTANCE.getUY();
		Float lY = QueryPanelControler.INSTANCE.getLY();
		

		
		if(LX == null || rX == null || uY == null || lY == null){
			
			JOptionPane.showMessageDialog(null, "Not all points are valid");
			return;
		}
		
		
		if(rX < LX){
			
			JOptionPane.showMessageDialog(null, "Right X can't be < Left X");
			return;
		}
		
		if(lY < uY){
			
			JOptionPane.showMessageDialog(null, "Lower Y can't be < Upper Y");
			return;			
		}		
		
		
		//show the loading panel in case it's $$ expensive, the worker will 
		// change back to the normal 'ready' panel once the query is complete
		QueryDialogController.INSTANCE.displayPanelType(QueryDialogController.PANEL_TYPE.LOADING);
		
		//get this expensive $$ process off the event dispatch thread
		// by putting it on a background swing worker
		new QueryExecutorWorker().execute();
		
	}
	
	
	
}

